<!--
  功能说明：用户列表页面，演示分页加载与展示。
-->
<template>
  <div>
    <h2>用户列表</h2>
    <table border="1" cellpadding="6">
      <thead>
        <tr>
          <th>ID</th>
          <th>姓名</th>
          <th>邮箱</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="u in items" :key="u.id">
          <td>{{ u.id }}</td>
          <td>{{ u.name }}</td>
          <td>{{ u.email }}</td>
        </tr>
      </tbody>
    </table>
    <Pagination :total="total" :page="page" :pageSize="pageSize" @change="onPage" />
  </div>
</template>

<script>
import { fetchUsers } from '../api/users'
import Pagination from '../components/Pagination.vue'
import { CONFIG } from '../config'

export default {
  name: 'UsersPage',
  components: { Pagination },
  data() {
    return {
      items: [],
      total: 0,
      page: 1,
      pageSize: CONFIG.PAGE_SIZE
    }
  },
  mounted() {
    this.load()
  },
  methods: {
    async load() {
      const res = await fetchUsers(this.page, this.pageSize)
      this.items = res.items
      this.total = res.total
    },
    onPage(p) {
      this.page = p
      this.load()
    }
  }
}
</script>